package com.baidu.controller;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.baidu.entity.User;
import com.baidu.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpSession;
import java.util.List;


/**
 * @描述
 * @Author UserController
 * @Date 2019/4/16 15:28
 **/
@Controller
@RequestMapping("/user")
public class UserController {


    @Autowired
    private UserService userService;


    /**
     * 根据id查询用户数据信息
     * @return
     */
    @RequestMapping("/selectUserById")
    public String selectUserById(Integer uid){
        User user = userService.selectUserById(uid);
        System.out.println(user);
        return null;
    }

    /**
     * 根据手机号查询
     * @param uphone
     * @return
     */
    @RequestMapping("/selectUseruphone")
    public Boolean selectUseruphone(String uphone){

        return null;
    }
    /**
     * 手机验证码接受以及验证
     * @param uphone
     * @param
     * @return
     */
    @RequestMapping("/uphoneCome")
    @ResponseBody
    public Boolean phoneBynum(HttpSession session,String uphone) throws ClientException {
        System.out.println(uphone);
        int num;
        num=(int)(Math.random()*89999)+100000;
        System.out.println(num+"---------------------------------");
        DefaultProfile profile = DefaultProfile.getProfile("default", "LTAIKcjNO3ZYpOSF", "S1AuqOcAymB90rsC1vOvP6eCw8C0e9");
        IAcsClient client = new DefaultAcsClient(profile);
        CommonRequest request = new CommonRequest();
        //request.setProtocol(ProtocolType.HTTPS);
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", uphone);
        request.putQueryParameter("TemplateCode", "SMS_163853446");
        request.putQueryParameter("SignName", "即兴生活馆");
        request.putQueryParameter("TemplateParam", "{code:"+num+"}");
        session.setAttribute("num",num);
        System.out.println(num+"===================================");
        CommonResponse response = client.getCommonResponse(request);
        System.out.println(response);
        return  true;
    }

    @RequestMapping("/login")
    @ResponseBody
    public Boolean Login(String uphone,Integer code,HttpSession session){

        System.out.println(uphone);
        System.out.println(code);
        /**
          * 查询用户对比手机号是否存在
          */
        int  co= (Integer) session.getAttribute("num");
        if(code==co){
            List<User> list = (List<User>) userService.selectUser();
            for (User user2 :list ) {
                user2.getUphone();
                if(uphone.equals(user2.getUphone())){
                    System.out.println("可以");
                    return true;
                }else{
                    System.out.println("注册");
                    Integer i = userService.registerUser(uphone);
                    if(i>0){
                        return true;
                    }else {
                        return false;
                    }
                }
            }
        }else{
            System.out.println("验证码出错！");
        }
        return  false;
    }
}
